﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsGame4
{
    class Player_ApproximateState : State
    {
        private static Player_ApproximateState instance;

        private Player_ApproximateState()
        {
            base.Name = "ApproximateState";
        }

        public override void enter(AIObject aiObject)
        {
            AIPlayer aiPlayer = (AIPlayer)aiObject;
            Player player = (Player)aiPlayer.Player;
            GameInfo gameInfo = GameInfo.getInstance();
      
            // Check Position player
            int posX = player.Position.X;
            int posY = player.Position.Y;
            switch (player.Direction.getDirection())
            {
                case "left":
                    {
                        int endPosX = ((posX / 64) * 64);
                        int endPosY = posY;
                        player.Movement = new Position2D(endPosX, endPosY);
                        break;
                    }
                case "right":
                    {
                        int endPosX = (((posX / 64)+1) * 64);
                        int endPosY = posY;
                        player.Movement = new Position2D(endPosX, endPosY);
                        break;
                    }
                case "down":
                    {
                        int endPosX = posX;
                        int endPosY = (((posY / 64) + 1) * 64);
                        player.Movement = new Position2D(endPosX, endPosY);
                        break;
                    }
                case "up":
                    {
                        int endPosX = posX;
                        int endPosY = ((posY / 64) * 64);
                        player.Movement = new Position2D(endPosX, endPosY);
                        break;
                    }
            }
        }

        public static Player_ApproximateState getInstance()
        {
            if (instance == null)
                instance = new Player_ApproximateState();
            return instance;
        }

        public override void execute(AIObject aiObject)
        {
            AIPlayer aiPlayer = (AIPlayer)aiObject;
            Player player = (Player)aiPlayer.Player;
            GameInfo gameInfo = GameInfo.getInstance();
            /*
            Position2D previousPosition = new Position2D(player.Position.X, player.Position.Y);
            System.Console.WriteLine("floipas");
            if (player.Movement != null)
            {
                int dx = player.Movement.X - player.Position.X;
                int dy = player.Movement.Y - player.Position.Y;

                if (dx == 0 && dy == 0)
                    player.Movement = null;
                else
                {
                    if (dx != 0)
                    {
                        if (Math.Abs(dx) < player.Speed)
                            player.Position.X += Math.Abs(dx) * Math.Sign(dx);
                        else
                            player.Position.X += player.Speed * Math.Sign(dx);
                    }
                    if (dy != 0)
                    {
                        if (Math.Abs(dy) < player.Speed)
                            player.Position.Y += Math.Abs(dy) * Math.Sign(dy);
                        else
                            player.Position.Y += player.Speed * Math.Sign(dy);
                    }

                }
            }


            int rowPlayer = gameInfo.World.getNumRow(player.Position.Y);
            int columnPlayer = gameInfo.World.getNumColumn(player.Position.X);
            int previousRow = gameInfo.World.getNumRow(previousPosition.Y);
            int previousColumn = gameInfo.World.getNumColumn(previousPosition.X);
            if (rowPlayer != previousRow || columnPlayer != previousColumn)
            {
                Cell cell = gameInfo.World.get(previousRow, previousColumn);
                cell.Obj = null;
                cell = gameInfo.World.get(rowPlayer, columnPlayer);
                cell.Obj = player;
            }*/
            /*
            System.Console.WriteLine("entra");
            
            int rowPlayer = gameInfo.World.getNumRow(player.Position.Y);
            int columnPlayer = gameInfo.World.getNumColumn(player.Position.X);
            int previousRow = gameInfo.World.getNumRow(previousPosition.Y);
            int previousColumn = gameInfo.World.getNumColumn(previousPosition.X);
            if (rowPlayer != previousRow || columnPlayer != previousColumn)
            {
                Cell cell = gameInfo.World.get(rowPlayer, columnPlayer);
                if (cell.Obj != null)
                {
                    // Colision
                    player.Position.X = previousPosition.X;
                    player.Position.Y = previousPosition.Y;
                    player.Movement = null;
                }
                else
                {
                    cell = gameInfo.World.get(previousRow, previousColumn);
                    cell.Obj = null;
                    cell = gameInfo.World.get(rowPlayer, columnPlayer);
                    cell.Obj = player;
                }
                
            }
             
            */

           if (player.Movement == null)
                EventEmitter.getInstance().registerMessage(0, aiPlayer.Id, aiPlayer.Id, Message.MessageType.msg_stopApproximate);
         
        }

        public override void exit(AIObject aiObject)
        {
            AIPlayer aiPlayer = (AIPlayer)aiObject;
            Player player = (Player)aiPlayer.Player;
            GameInfo gameInfo = GameInfo.getInstance();

            player.Moving = false;
        }

        public override Boolean onMessage(AIObject aiObject, Message message)
        {
            Boolean correct = false;
            switch (message.Type)
            {

                case Message.MessageType.msg_stopApproximate:
                    {
                        aiObject.StateMachine.changeState(Player_StopState.getInstance());
                        correct = true;
                        break;
                    }
            }
            return correct;
        }
    }
}

